package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.entity.AnswerEntity;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class AnswerPopupWindow extends PopupWindow {
    private Context context;
    private View layout;
    private List<AnswerEntity.DataBean> list;
    private TextView tv_answer_count, tv_answer_desc, tv_a, tv_b, tv_c, tv_d, tv_from;
    private RelativeLayout rl_a, rl_b, rl_c, rl_d;
    private ImageView iv_a, iv_b, iv_c, iv_d, iv_dismiss;
    private ImageView iv_right_answer_a, iv_right_answer_b, iv_right_answer_c, iv_right_answer_d;
    private TextView tv_time;
    private TextView tv_title;
    private LinearLayout ll_content, ll_answer_fail;
    private int answerCount = 0;
    private Timer timer;
    private TimerTask timerTask;
    private int timeCount = 3;
    private int answerRightCount = 0;//答题正确的数量
    private Inter inter;
    public static final String A = "A";
    public static final String B = "B";
    public static final String C = "C";
    public static final String D = "D";

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                String str = "%ss 进入下一题";
                tv_time.setText(String.format(str, timeCount));
            } else if (msg.what == 2) {
                answerCount++;
                timeCount = 3;
                setView(answerCount);
                if (timer != null) {
                    timer.cancel();
                    timer = null;
                }
                if (timerTask != null) {
                    timerTask.cancel();
                    timerTask = null;
                }
            } else if (msg.what == 3) {
                String str = "%ss 查看答题结果";
                tv_time.setText(String.format(str, timeCount));
            } else if (msg.what == 4) {
                if (timer != null) {
                    timer.cancel();
                    timer = null;
                }
                if (timerTask != null) {
                    timerTask.cancel();
                    timerTask = null;
                }
                if (inter != null) {
                    if (answerRightCount == 0) {
                        tv_title.setText("答题失败");
                        ll_answer_fail.setVisibility(View.VISIBLE);
                        ll_content.setVisibility(View.GONE);
                        iv_dismiss.setVisibility(View.VISIBLE);
                    } else {
                        if (inter != null) {
                            inter.inter(answerRightCount);
                            dismiss();
                        }
                    }
                }
            }
        }
    };

    public static AnswerPopupWindow getInstance(Context context, List<AnswerEntity.DataBean> list, Inter inter) {
        return new AnswerPopupWindow(context, list, inter);
    }

    private AnswerPopupWindow(Context context, List<AnswerEntity.DataBean> list, Inter inter) {
        super(context);
        this.context = context;
        this.list = list;
        this.inter = inter;
        answerRightCount = 0;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(false);
        this.setOutsideTouchable(false);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);

        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_answer, null);
        tv_answer_count = layout.findViewById(R.id.tv_answer_count);
        tv_answer_desc = layout.findViewById(R.id.tv_answer_desc);
        tv_a = layout.findViewById(R.id.tv_a);
        tv_b = layout.findViewById(R.id.tv_b);
        tv_c = layout.findViewById(R.id.tv_c);
        tv_d = layout.findViewById(R.id.tv_d);
        tv_from = layout.findViewById(R.id.tv_from);
        rl_a = layout.findViewById(R.id.rl_a);
        rl_b = layout.findViewById(R.id.rl_b);
        rl_c = layout.findViewById(R.id.rl_c);
        rl_d = layout.findViewById(R.id.rl_d);
        iv_a = layout.findViewById(R.id.iv_a);
        iv_b = layout.findViewById(R.id.iv_b);
        iv_c = layout.findViewById(R.id.iv_c);
        iv_d = layout.findViewById(R.id.iv_d);
        tv_time = layout.findViewById(R.id.tv_time);
        tv_title = layout.findViewById(R.id.tv_title);
        ll_content = layout.findViewById(R.id.ll_content);
        ll_answer_fail = layout.findViewById(R.id.ll_answer_fail);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        iv_right_answer_a = layout.findViewById(R.id.iv_right_answer_a);
        iv_right_answer_b = layout.findViewById(R.id.iv_right_answer_b);
        iv_right_answer_c = layout.findViewById(R.id.iv_right_answer_c);
        iv_right_answer_d = layout.findViewById(R.id.iv_right_answer_d);

        setView(answerCount);
    }

    private void setTimer() {
        timer = new Timer();
        timerTask = new TimerTask() {
            @Override
            public void run() {
                timeCount--;
                if (timeCount == 0) {
                    if (answerCount == 0)
                        mHandler.sendEmptyMessage(2);
                    else {
                        //查看结果
                        mHandler.sendEmptyMessage(4);
                    }
                } else {
                    if (answerCount == 0)
                        mHandler.sendEmptyMessage(1);
                    else
                        mHandler.sendEmptyMessage(3);
                }
            }
        };
        timer.schedule(timerTask, 0, 1000);
    }

    private void setEnable(boolean enable) {
        rl_a.setEnabled(enable);
        rl_b.setEnabled(enable);
        rl_c.setEnabled(enable);
        rl_d.setEnabled(enable);
    }

    private void setGone() {
        iv_a.setVisibility(View.GONE);
        iv_b.setVisibility(View.GONE);
        iv_c.setVisibility(View.GONE);
        iv_d.setVisibility(View.GONE);
    }

    private void setBackGround() {
        rl_a.setBackgroundResource(R.drawable.bg_f5f6f8_18dp);
        rl_b.setBackgroundResource(R.drawable.bg_f5f6f8_18dp);
        rl_c.setBackgroundResource(R.drawable.bg_f5f6f8_18dp);
        rl_d.setBackgroundResource(R.drawable.bg_f5f6f8_18dp);
    }

    private void setTextColor(int resId) {
        tv_a.setTextColor(context.getResources().getColor(resId));
        tv_b.setTextColor(context.getResources().getColor(resId));
        tv_c.setTextColor(context.getResources().getColor(resId));
        tv_d.setTextColor(context.getResources().getColor(resId));
    }

    private void setTextColor(TextView tv) {
        tv.setTextColor(context.getResources().getColor(R.color.white));
    }

    private void setRightAnswerShow(String rightAnswer) {
        switch (rightAnswer) {
            case A:
                iv_right_answer_a.setVisibility(View.VISIBLE);
                break;
            case B:
                iv_right_answer_b.setVisibility(View.VISIBLE);
                break;
            case C:
                iv_right_answer_c.setVisibility(View.VISIBLE);
                break;
            case D:
                iv_right_answer_d.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void setRightAnswerHidden() {
        iv_right_answer_a.setVisibility(View.GONE);
        iv_right_answer_b.setVisibility(View.GONE);
        iv_right_answer_c.setVisibility(View.GONE);
        iv_right_answer_d.setVisibility(View.GONE);
    }

    private void setView(int answerCount) {
        if (list != null && list.size() > 0) {
            String str = "%s/%s题";
            tv_answer_count.setText(String.format(str, answerCount + 1, list.size()));
            tv_answer_desc.setText(list.get(answerCount).question);
            tv_answer_desc.setText(list.get(answerCount).question);

            setTextColor(R.color.color_585858);
            setBackGround();
            setEnable(true);
            setGone();
            setRightAnswerHidden();
            iv_dismiss.setVisibility(View.GONE);

            tv_time.setVisibility(View.GONE);

            String from = "题目来源 - %s";
            tv_from.setText(String.format(from, list.get(answerCount).questionType == 1 ? "长征学院" : "党史"));

            if (!TextUtils.isEmpty(list.get(answerCount).answerA)) {
                tv_a.setText(list.get(answerCount).answerA);
                tv_a.setVisibility(View.VISIBLE);
            } else
                tv_a.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(list.get(answerCount).answerB)) {
                tv_b.setText(list.get(answerCount).answerB);
                tv_b.setVisibility(View.VISIBLE);
            } else
                tv_b.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(list.get(answerCount).answerC)) {
                tv_c.setText(list.get(answerCount).answerC);
                tv_c.setVisibility(View.VISIBLE);
            } else
                tv_c.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(list.get(answerCount).answerD)) {
                tv_d.setText(list.get(answerCount).answerD);
                tv_d.setVisibility(View.VISIBLE);
            } else
                tv_d.setVisibility(View.GONE);
        }
    }


    private void initEvent() {
        rl_a.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTextColor(tv_a);
                tv_time.setVisibility(View.VISIBLE);
                setEnable(false);
                setTimer();
                if (list.get(answerCount).rightAnswer.equals(A)) {
                    rl_a.setBackgroundResource(R.drawable.bg_44da7a_18dp);
                    iv_a.setVisibility(View.VISIBLE);
                    iv_a.setImageResource(R.mipmap.ico_answer_right);
                    answerRightCount++;
                } else {
                    rl_a.setBackgroundResource(R.drawable.bg_fc6c6b_18dp);
                    iv_a.setVisibility(View.VISIBLE);
                    iv_a.setImageResource(R.mipmap.ico_answer_miss);
                    setRightAnswerShow(list.get(answerCount).rightAnswer);
                }
            }
        });
        rl_b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTextColor(tv_b);
                tv_time.setVisibility(View.VISIBLE);
                setEnable(false);
                setTimer();
                if (list.get(answerCount).rightAnswer.equals(B)) {
                    rl_b.setBackgroundResource(R.drawable.bg_44da7a_18dp);
                    iv_b.setVisibility(View.VISIBLE);
                    iv_b.setImageResource(R.mipmap.ico_answer_right);
                    answerRightCount++;
                } else {
                    rl_b.setBackgroundResource(R.drawable.bg_fc6c6b_18dp);
                    iv_b.setVisibility(View.VISIBLE);
                    iv_b.setImageResource(R.mipmap.ico_answer_miss);
                    setRightAnswerShow(list.get(answerCount).rightAnswer);
                }
            }
        });
        rl_c.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTextColor(tv_c);
                tv_time.setVisibility(View.VISIBLE);
                setEnable(false);
                setTimer();
                if (list.get(answerCount).rightAnswer.equals(C)) {
                    rl_c.setBackgroundResource(R.drawable.bg_44da7a_18dp);
                    iv_c.setVisibility(View.VISIBLE);
                    iv_c.setImageResource(R.mipmap.ico_answer_right);
                    answerRightCount++;
                } else {
                    rl_c.setBackgroundResource(R.drawable.bg_fc6c6b_18dp);
                    iv_c.setVisibility(View.VISIBLE);
                    iv_c.setImageResource(R.mipmap.ico_answer_miss);
                    setRightAnswerShow(list.get(answerCount).rightAnswer);
                }
            }
        });
        rl_d.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setTextColor(tv_d);
                tv_time.setVisibility(View.VISIBLE);
                setEnable(false);
                setTimer();
                if (list.get(answerCount).rightAnswer.equals(D)) {
                    rl_d.setBackgroundResource(R.drawable.bg_44da7a_18dp);
                    iv_d.setVisibility(View.VISIBLE);
                    iv_d.setImageResource(R.mipmap.ico_answer_right);
                    answerRightCount++;
                } else {
                    rl_d.setBackgroundResource(R.drawable.bg_fc6c6b_18dp);
                    iv_d.setVisibility(View.VISIBLE);
                    iv_d.setImageResource(R.mipmap.ico_answer_miss);
                    setRightAnswerShow(list.get(answerCount).rightAnswer);
                }
            }
        });
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (inter != null)
                    inter.inter(answerRightCount);
                dismiss();
            }
        });
    }


    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }


    public interface Inter {
        void inter(int count);
    }
}
